package com.cfx.exercise_46;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jbarcode.util.ImageUtil;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfCell;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 生成公章刻制回执单
 * @author zhangs
 *
 */
public class PDFReceipt {
	
	Document doc = new Document();
	private ByteArrayOutputStream ba;
	private static final String CHARSET = "utf-8";
    private static final int QRCODE_WIDTH = 50; // 二维码尺寸宽度
    private static final int QRCODE_HEIGHT = 50; // 二维码尺寸宽度
	private static Font titleFont;// 设置字体大小
	private static Font keyFont;
	static {
		BaseFont bfChinese;
		try {
			bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, true);
			titleFont = new Font(bfChinese, 18, Font.BOLD);// 设置字体大小
			keyFont = new Font(bfChinese, 10, Font.NORMAL);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PDFReceipt(File file) {
		doc.setPageSize(PageSize.A4);// 设置页面大小
		try {
			PdfWriter.getInstance(doc, new FileOutputStream(file));
			doc.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ByteArrayOutputStream getBa() {
		return ba;
	}

	public void setBa(ByteArrayOutputStream ba) {
		this.ba = ba;
	}

	public PDFReceipt() {
		HeaderFooter footer = new HeaderFooter(new Phrase("第 ", keyFont), new Phrase(" 页", keyFont));
		footer.setAlignment(Element.ALIGN_CENTER);
		footer.setBorder(Rectangle.NO_BORDER);
		doc.setFooter(footer);
		ba = new ByteArrayOutputStream();
		doc.setPageSize(PageSize.A4);// 设置页面大小
		doc.setMargins(50, 50, 50, 50);
		try {
			PdfWriter.getInstance(doc, ba);
			doc.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Image getImage(String content, int x, int y, float w, float h, boolean b) {
		BufferedImage image = null;
		Image code128Image = null;
		try {
			image = createImage(content);
			code128Image = Image.getInstance(ImageUtil.encode(image, "png"));
			code128Image.setAlignment(Image.MIDDLE);// 浮动排版
			code128Image.setAbsolutePosition(x, y);// 页面绝对定位坐标
			code128Image.scaleAbsolute(w, h);// 设置宽高
		} catch (Exception e) {
			e.printStackTrace();
		}
		return code128Image;
	}
	public BufferedImage createImage(String content) throws Exception{
		Map<EncodeHintType, Comparable<?>> hints = new HashMap<EncodeHintType, Comparable<?>>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, QRCODE_WIDTH, QRCODE_HEIGHT, hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
	}
	public void onEndPage(PdfWriter writer, Document document) {

		Rectangle rect = writer.getBoxSize("art");
		switch (writer.getPageNumber() % 2) {
		case 0:
			ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase("even header"),
					rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0);
			break;
		case 1:
			ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER,
					new Phrase(String.format("%d", writer.getPageNumber())), 300f, 62f, 0);
			break;
		}
	}
	
	/**
	 * @param record 备案单编码
	 * @param companyName 企业名称 
	 * @param companyEnglishName 企业英文名称
	 * @param companyAddress 单位地址
	 * @param companyArnCode 统一社会信用代码
	 * @param sealReason 刻制原因
	 * @param phone 
	 * @param companyType 单位类型
	 * @param companyLeaderName 法人
	 * @param companyLeaderCertificateNumber 法人证件号码
	 * @param consignerName 经办人姓名
	 * @param consignerIdCard 经办人证件号码
	 * @param sealList 印章list （String[] strings = new String{sealCode,sealName}）
	 * @param factoryName 刻制企业名称
	 * @param factorytelphone 刻制企业电话
	 * @param qrCodecontent 二维码内容
	 * @throws Exception
	 */
	public void getPDF(String record,String companyName, String companyEnglishName, String companyAddress, String companyArnCode,
			String sealReason, String phone, String companyType, String companyLeaderName,
			String companyLeaderCertificateNumber, String consignerName, String consignerIdCard,
			List<String[]> sealList, String factoryName, String factorytelphone, String qrCodecontent) throws Exception {
		Paragraph title = new Paragraph("公 章 刻 制 回 执 单", titleFont);//标题
		title.setAlignment(Element.ALIGN_CENTER);// 设置居中
		LocalDate ld = LocalDate.now();
		Paragraph dateText = new Paragraph("填表时间：\t    \t\t" + ld.getYear() + " 年 " + ld.getMonthValue() + "  月 " + ld.getDayOfMonth() + "  日                                                                                       备案单编码："+record,keyFont);//填报日期
		dateText.setSpacingBefore(30f);
		dateText.setSpacingAfter(5f);
		PdfPTable table = new PdfPTable(1);
		PdfPCell cell = new PdfPCell();
		cell.setBorder(0);
		table.setWidthPercentage(100f);
		table.setSplitLate(false);// 自动换页
		PdfPTable table1 = new PdfPTable(6);
		table1.setWidthPercentage(100f);
		table1.setWidths(new float[] {10f,18f,7f,13f,12f,40f});
		PdfPCell t1c1 = new PdfPCell(new Paragraph("公\n\n章\n\n基\n\n本\n\n信\n\n息",keyFont));//公众基本信息单元格
		t1c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		t1c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c1.setRowspan(sealList.size() + 9);
		PdfPCell t1c2 = new PdfPCell(new Paragraph(" 单 位 名 称",keyFont));//单位名称
		t1c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c2.setRowspan(2);
		PdfPCell t1c3 = new PdfPCell(new Paragraph(" 中文",keyFont));//单位名称
		t1c3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c4 = new PdfPCell(new Paragraph(companyName, keyFont));
		t1c4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c4.setColspan(3);
		t1c4.setMinimumHeight(20f);
		PdfPCell t1c5 = new PdfPCell(new Paragraph(" 英文",keyFont));//单位名称
		t1c5.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c6 = new PdfPCell(new Paragraph(companyEnglishName, keyFont));
		t1c6.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c6.setColspan(3);
		t1c6.setMinimumHeight(20f);
		PdfPCell t1c7 = new PdfPCell(new Paragraph(" 单 位 地 址",keyFont));
		t1c7.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c8 = new PdfPCell(new Paragraph(companyAddress, keyFont));
		t1c8.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c8.setColspan(4);
		t1c8.setMinimumHeight(20f);
		PdfPCell t1c9 = new PdfPCell(new Paragraph(" 统一社会信用代码",keyFont));
		t1c9.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c10 = new PdfPCell(new Paragraph(companyArnCode, keyFont));
		t1c10.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c9.setMinimumHeight(20f);
		t1c10.setColspan(2);
		PdfPCell t1c11 = new PdfPCell(new Paragraph(" 刻 制 原 因",keyFont));
		t1c11.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c12 = new PdfPCell(new Paragraph(sealReason, keyFont));
		t1c12.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c13 = new PdfPCell(new Paragraph(" 联 系 电 话",keyFont));
		t1c13.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c14 = new PdfPCell(new Paragraph(phone,keyFont));
		t1c14.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c14.setColspan(2);
		t1c14.setMinimumHeight(20f);
		PdfPCell t1c15 = new PdfPCell(new Paragraph(" 单 位 类 型",keyFont));
		t1c15.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c16 = new PdfPCell(new Paragraph(companyType, keyFont));
		t1c16.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c17 = new PdfPCell(new Paragraph(" 法 定 代 表 人",keyFont));
		t1c17.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c18 = new PdfPCell(new Paragraph(companyLeaderName, keyFont));
		t1c18.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c17.setMinimumHeight(20f);
		t1c18.setColspan(2);
		PdfPCell t1c19 = new PdfPCell(new Paragraph(" 身 份 证 号",keyFont));
		t1c19.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c20 = new PdfPCell(new Paragraph(companyLeaderCertificateNumber, keyFont));
		t1c20.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c21 = new PdfPCell(new Paragraph(" 经  办  人",keyFont));
		t1c21.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c22 = new PdfPCell(new Paragraph(consignerName, keyFont));
		t1c22.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c21.setMinimumHeight(20f);
		t1c22.setColspan(2);
		PdfPCell t1c23 = new PdfPCell(new Paragraph(" 身 份 证 号",keyFont));
		t1c23.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c24 = new PdfPCell(new Paragraph(consignerIdCard, keyFont));
		t1c24.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c25 = new PdfPCell(new Paragraph("印 章 编 码",keyFont));
		t1c25.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c25.setHorizontalAlignment(Element.ALIGN_CENTER);
		t1c25.setMinimumHeight(20f);
		t1c25.setColspan(3);
		PdfPCell t1c26 = new PdfPCell(new Paragraph("刻 制 公 章 全 称",keyFont));
		t1c26.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c26.setHorizontalAlignment(Element.ALIGN_CENTER);
		t1c26.setColspan(2);
		doc.add(title);
		doc.add(dateText);
		table1.addCell(t1c1);
		table1.addCell(t1c2);
		table1.addCell(t1c3);
		table1.addCell(t1c4);
		table1.addCell(t1c5);
		table1.addCell(t1c6);
		table1.addCell(t1c7);
		table1.addCell(t1c8);
		table1.addCell(t1c9);
		table1.addCell(t1c10);
		table1.addCell(t1c11);
		table1.addCell(t1c12);
		table1.addCell(t1c13);
		table1.addCell(t1c14);
		table1.addCell(t1c15);
		table1.addCell(t1c16);
		table1.addCell(t1c17);
		table1.addCell(t1c18);
		table1.addCell(t1c19);
		table1.addCell(t1c20);
		table1.addCell(t1c21);
		table1.addCell(t1c22);
		table1.addCell(t1c23);
		table1.addCell(t1c24);
		table1.addCell(t1c25);
		table1.addCell(t1c26);
		int k = 0;
		PdfPCell t1cn1 = new PdfPCell();
		t1cn1.setColspan(5);
		
		for (String[] strings : sealList) {
			k++;
			PdfPCell t1cn3 = new PdfPCell(new Paragraph(strings[0],keyFont));
			PdfPCell t1cn4 = new PdfPCell(new Paragraph(strings[1],keyFont));
			t1cn3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
			t1cn3.setHorizontalAlignment(Element.ALIGN_LEFT);
			t1cn3.setColspan(3);
			t1cn3.setMinimumHeight(20f);
			t1cn4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
			t1cn4.setHorizontalAlignment(Element.ALIGN_LEFT);
			t1cn4.setColspan(2);
			t1cn4.setMinimumHeight(20f);
			table1.addCell(t1cn3);
			table1.addCell(t1cn4);
			if(k == sealList.size()){
				switch (k) {
				case 1:
					t1cn1.setFixedHeight(300);
					break;
				case 2:
					t1cn1.setFixedHeight(280);
					break;
				case 3:
					t1cn1.setFixedHeight(260);
					break;
				case 4:
					t1cn1.setFixedHeight(240);
					break;
				case 5:
					t1cn1.setFixedHeight(220);
					
					break;
				case 6:
					t1cn1.setFixedHeight(200);
					break;
				case 7:
					t1cn1.setFixedHeight(180);
					break;
				case 8:
					t1cn1.setFixedHeight(160);
					break;
				case 9:
					t1cn1.setFixedHeight(140);
					break;
				case 10:
					t1cn1.setFixedHeight(120);
					break;
				case 11:
					t1cn1.setFixedHeight(100);
					break;
				case 12:
					t1cn1.setFixedHeight(80);
					break;
				case 14:
					t1cn1.setFixedHeight(60);
					break;
				case 15:
					t1cn1.setFixedHeight(40);
					break;
				case 16:
					t1cn1.setFixedHeight(20);
					break;
				default:
					break;
				}
			}
			
		}
		if(k<17){
		table1.addCell(t1cn1);
		
		}
		
		PdfPTable table2 = new PdfPTable(4);
		table2.setWidthPercentage(100f);
		table2.setWidths(new float[]{10f,20f,50f,20f});
		PdfPCell t2c1 = new PdfPCell(new Paragraph("\n刻\n\n制\n\n单\n\n位",keyFont));
		t2c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		t2c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c1.setRowspan(3);
		PdfPCell t2c2 = new PdfPCell(new Paragraph("\t以上\t    \t\t\t枚公章已办理备案手续",keyFont));
		t2c2.setMinimumHeight(40f);
		t2c2.setColspan(3);
		t2c2.setVerticalAlignment(PdfPCell.ALIGN_BOTTOM);
		t2c2.setBorder(8);
		PdfPCell t2c3 = new PdfPCell();
		t2c3.addElement(getImage(qrCodecontent, 130, 250, 90, 90, false));
		t2c3.setRowspan(2);
		t2c3.setBorder(2);
		t2c3.setHorizontalAlignment(Element.ALIGN_CENTER);
		t2c3.setVerticalAlignment(Element.ALIGN_MIDDLE);
		PdfPCell t2c6 = new PdfPCell(new Paragraph("刻制企业名称：" + factoryName, keyFont));
		t2c6.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c6.setFixedHeight(50f);
		t2c6.setBorder(0);
		PdfPCell t2c7 = new PdfPCell(new Paragraph("\n（盖章）",keyFont));
		t2c7.setBorder(8);
		PdfPCell t2c8 = new PdfPCell(new Paragraph("刻制企业电话："+ factorytelphone,keyFont));
		t2c8.setBorder(2);
		t2c8.setFixedHeight(60f);
		PdfPCell t2c9 = new PdfPCell(new Paragraph("年          月          日",keyFont));
		t2c9.setVerticalAlignment(PdfCell.ALIGN_BOTTOM);
		t2c9.setBorder(10);
		table2.addCell(t2c1);
		table2.addCell(t2c2);
		table2.addCell(t2c3);
		table2.addCell(t2c6);
		table2.addCell(t2c7);
		table2.addCell(t2c8);
		table2.addCell(t2c9);
		cell.addElement(table1);
		cell.addElement(table2);
		table.addCell(cell);
		/*doc.add(table1);
		doc.add(table2);*/
		doc.add(table);
		doc.close();
	}
	public static void main(String[] args) throws DocumentException, Exception {
		PDFReceipt pdf = new PDFReceipt(new File("d://pdf.pdf"));
		String record = "1231241412342";
		String zn = "北京科富兴科技有限公司";
		String en = "abc";
		String address = "北京市丰台区时代财富大厦1702";
		String number = "56481257845639215Y";
		String yy = "公司财务需要";
		String phone = "13756238469";
		String type = "国企";
		String comper = "张三丰";
		String id = "413264786541239875";
		String comperper = "张无忌";
		String id2 = "411424199004065953";
		String name = "丰台有名刻制社丰台有名刻制社丰台有名刻制社丰台有名刻制社丰台有名刻制社丰台有名刻制社";
		String tel = "15823648596";
		String string = new String("财务专用章财务专用章财务专用章");
		String string1 = new String("6549876435164");
		List<String[]> list = new ArrayList<>();
		for(int i =0;i<40;i++){
			String[] strings = {string1,string};
			list.add(strings);
		}
		pdf.getPDF(record,zn,en,address,number,yy,phone,type,comper,id,comperper,id2,list,name,tel, "qiye");
	}
}
